package cn.wmw.xyy;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.factory.Factory.Payment;
import com.alipay.easysdk.kernel.Config;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.facetoface.models.AlipayTradePrecreateResponse;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;

public class Main {

    public static void main(String[] args) throws Exception {
        // 1. 设置参数（全局只需设置一次）
        Factory.setOptions(getOptions());
        try {
            // 2. 发起API调用（以创建当面付收款二维码为例）
            //AlipayTradePrecreateResponse response = Payment.FaceToFace().preCreate("Apple iPhone11 128G", "2234567890", "5799.00");
            AlipayTradePagePayResponse response = Payment.Page().pay("测试支付宝支付！", "21984673218947623642736", "1000.00", "http://127.0.0.1:6002/detail.html?courseId=22");
            // 3. 处理响应或异常
            if (ResponseChecker.success(response)) {
                System.out.println("调用成功" + response.getBody());
            } else {
                System.err.println("调用失败，原因：" + response.getBody() + "，" + response.toString());
            }
        } catch (Exception e) {
            System.err.println("调用遭遇异常，原因：" + e.getMessage());
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    private static Config getOptions() {
        Config config = new Config();
        config.protocol = "https";
        config.gatewayHost = "openapi.alipaydev.com";
        config.signType = "RSA2";
        config.appId = "2021000122605082";
        // 为避免私钥随源码泄露，推荐从文件中读取私钥字符串而不是写入源码中
        config.merchantPrivateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCR7lg5JD1k6Si/pFDqHgrFaMr6YKVGOQmz5YI8HsCZoIynPiXnssyUmIi3iggr32bdi81xlq6O5HnpAhykzAB7Gs801GFBEkvDRKiexC+AP8xFIEmd5koMO4odv7/cMT3lTAFHDLFn7iiNW0sPPek+UWzeteu00LICW3M9yIGY5xXjNcxhNU+5IDxqn7AZME2GK/UlSrI8uDDJFxQynRba22m2TQpsMC09h6G+/x5UqTjoTURg9SpBeuGccp3gcoQM8a1a1ZSj2ZG9W9FtuVpQFB9hLWyomLmQd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eVPK85KXLRvONyuGJRf3GUYk7pQKBgD72dk2gvpMGw4czBhtCzxuma5cSisTr4QtzFdT8NzpcODvmZVqbk2Vk9S98vf1KfimVQCA5+kWNdRheKSG3h910H5rYDII2cz4UTimzDQNm3XKfZpuf0y+0bJS15SbvYrMo27GzwS6ydyeKamNafQaXlhwq66IGz03VQ/hYEgrQ";
        //注：如果采用非证书模式，则无需赋值上面的三个证书路径，改为赋值如下的支付宝公钥字符串即可
        config.alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAke5YOSQ9ZOkov6RQ6h4KxWjK+mClRjkJs+WCPB7AmaCMpz4l57LMlJiIt4oIK99m3YvNcZaujuR56QIcpMwAexrPNNRhQRJLw0SonsQvgD/MRSBJneZKDDuKHb+/3DE95UwBRwyxZ+4ojVtLDz3pPlFs3rXrtNCyAltzPciBmOcV4zXMYTVPuSA8ap+wGTBNhiv1JUqyPLgwyRcUMp0W2ttptk0KbDAtPYehvv8eVKk46E1EYPUqQXrhnHKd4HKEDPGtWtWUo9mRvVvRbblaUBQfYS1sqJi5kHWOERPbknP04dXgzF2NrevobrDi7FlvXzeeT3ciABKl3pMCbWWkHwIDAQAB";
        //可设置异步通知接收服务地址（可选）
        config.notifyUrl = "www.baidu.com";
        return config;
    }

}